package com.itheima.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.itheima.interceptor.MyInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.Arrays;

/**
 * SpringMvc的核心配置文件
 * @author chenshan
 */
@Configuration
public class SpringMvcConfig implements WebMvcConfigurer {
    /**
     * 注册拦截器
     * addPathPatterns：添加拦截路径 拦截所有
     * excludePathPatterns：排除被拦截的资源
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new MyInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns(Arrays.asList("/**/*.css","/**/*.js","/plugins/**", "/index.html"))
                .addPathPatterns();
    }

    /**
     * 分页查询拦截器
     * @return MybatisPlusInterceptor
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        // 创建MP的拦截器对象
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        // 创建MP的分页拦截器，并设置方言（对哪个数据库进行分页查询）
        PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor(DbType.MYSQL);
        // 设置请求的页面大于最大页后操作，true-调回到首页，false-继续请求 默认false
        paginationInnerInterceptor.setOverflow(true);
        // 设置最大单页限制数量 -1不受限制
        paginationInnerInterceptor.setMaxLimit(-1L);
        interceptor.addInnerInterceptor(paginationInnerInterceptor);

        return interceptor;
    }
}
